package com.xul.srb.sms.receiver;

import com.xul.srb.base.dto.SmsDTO;
import com.xul.srb.rabbitmq.constant.MQConst;
import com.xul.srb.sms.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author xul
 * @create 2022-08-06 16:41
 * <p>
 * rabbitmq 消费消息  并短信通知用户
 */

@Component
@Slf4j
public class SmsReceiver {

    @Resource
    private SmsService smsService;

    /**
     * rabbitmq 绑定 队列 交换机 routingKey
     *
     * @param smsDTO
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConst.QUEUE_SMS_ITEM, durable = "true"),
            exchange = @Exchange(value = MQConst.EXCHANGE_TOPIC_SMS),
            key = {MQConst.ROUTING_SMS_ITEM}
    ))

    public void send(SmsDTO smsDTO) {
        log.info("监听消息");
        // 发送短信提醒
        smsService.send(smsDTO.getMobile());
    }
}
